package rsj.api.org;

import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import common.util.Assert;
import common.util.BoxUtil;
import common.util.QueryRunnerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.user.BaseLink;
import rsj.service.IExecuteInterface;
import rsj.sql.org.QueryOrgKeyDao;

import java.sql.SQLException;

/**
 * @author yuanqinglong
 * @since 2021/3/30 17:48
 */
@RestController
public class QueryOrgKeyController extends BaseLink {

    Logger logger = LoggerFactory.getLogger(QueryOrgKeyController.class);

    @Autowired
    private QueryOrgKeyDao queryOrgKeyDao;


    @Override
    @RequestMapping(value = "rsj_query_org_key")
    public Box executeLink(Box box) throws SQLException {
        String orgId = box.getMapNode("data").getValue("org_id");
        Assert.isNotBlank(orgId, "机构id不可为空");
        return BoxUtil.successBox(box).putMapNode("data", queryOrgKeyDao.queryOrgKey(orgId));
    }


    /**
     * 查询机构认证信息
     *
     * @param orgId 机构id
     * @return 认证信息
     */
    private MapNode queryOrgKey(String orgId) {
        String sql = "SELECT aut_keyid,aut_secretkey,aut_signkey FROM sys.sys_authon WHERE aut_org_id = '" + orgId + "'";
        return (MapNode) IExecuteInterface.doExecuteQuery(connection ->
                QueryRunnerFactory.getQueryRunner().queryLinkRow(connection, sql), e -> logger.error("查询机构认证失败,{}", e));
    }
}
